import { queryType } from './index'

export default {
  toast (content, duration) {
    uni.showToast({
      title: content,
      icon: 'none',
      duration: duration || 1500,
      success () {
        setTimeout(() => {
          uni.hideToast()
        }, duration || 1500)
      }
    })
  },
  modal (title, content, showCancel, cancelText, confirmText) {
    if (typeof showCancel == 'undefined') {
      showCancel = true
    }
    return new Promise((resolve, reject) => {
      uni.showModal({
        title,
        content,
        showCancel,
        cancelText: cancelText || '取消',
        confirmText: confirmText || '确定',
        success (res) {
          if (res.confirm) {
            return resolve(true)
          }
          return resolve(false)
        }
      })
    })
  },
  back (delta) {
    if (typeof delta == 'undefined') {
      delta = 1
    }
    uni.navigateBack({
      delta
    })
  },
  go (page, url, data) {
    let pages = getCurrentPages()
    console.log('当前路由', pages)
    let ret = {}
    if (url && queryType(url) == 'String') {
      ret.url = '/subPackages/' + page + '/' + url + '/' + url
    } else {
      data = url
      ret.url = '/pages/' + page + '/' + page
    }
    if (data) {
      ret.url = ret.url + '?data=' + encodeURIComponent(JSON.stringify(data))
    }
    console.log('请求地址', '----------', ret)
    if (pages.length > 6) {
      uni.reLaunch(ret)
    } else {
      uni.navigateTo(ret)
    }
  },
  parseQuery (query) {
	  console.log('parseQuery',query)
    if (query.data) {
      return JSON.parse(decodeURIComponent(query.data))
    } else {
      return {}
    }
  }
}